<?php

namespace Apps\AppApi\Method\Order;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\UserOrderGoods;
use PhalconPlus\Api\Common\ResultStatus;
use Apps\Common\Models\UserOrder;


/**
 * 我的订单
 * @package Apps\Apps\AppApi\Method\Order
 */
class MyOrder extends BaseMethod
{
    public function run()
    {
        /**
         * 验证用户权限
         */
        if(false == $this->verifyUserAuth()){
            return false;
        }
        $type = $this->getDataItem('type', 'all');
        $page = intval($this->getDataItem('page', 0));
        $page = $page < 1 ? 1 : $page;
        $limit = $this->getConfig()->limit;
        $offset = ($page-1) * $limit;
        $userId = $this->getUserAuth()->userId;

        $typeArray = ['all', 'pay', 'end'];
        if(!in_array($type, $typeArray)) {
            return $this->errorLog(ResultStatus::POST_BODY_PARAM_ERROR, '查询的类型不存在.');
        }
        $where = 'user_id = :uid:';
        $whereParams = ['uid' => $userId];
        switch($type) {
            case 'all':
                $where .= ' AND order_state >= 0';
                break;
            case 'pay':
                $where .= ' AND order_state >= 0 AND order_pay_state = 0';
                break;
            case 'end':
                $where .= ' AND order_state >= 0 AND order_pay_state > 0';
                break;
        }

        $orders = UserOrder::query()
            ->columns('order_id, order_long_id, order_money, order_mobile, order_pay_type, order_pay_state, order_addtime')
            ->where($where, $whereParams)
            ->orderBy('order_id desc')
            ->limit($limit, $offset)
            ->execute()
            ->toArray();
        $data = [];
        foreach($orders as $order) {
            $order['order_pay_type'] = $this->getPayTypeStr($order['order_pay_type']);
            $order['order_pay_state'] = $order['order_pay_state'] > 0 ? 1 : 0;
            $order['order_addtime'] = date('Y-m-d', strtotime($order['order_addtime']));
            $order['goods'] = $this->getGoods($order['order_id']);
            $data[] = $order;
        }
        $this->setResult($data);
    }

    private function getGoods($orderId)
    {
        $goodsList = UserOrderGoods::query()
            ->columns([
                'shop_id',
                'bp.bp_name shop_name',
                'bp.bp_medal shop_medal_refund',
                'bp.bp_medal shop_medal_verify',
                'goods_id',
                'bps.service_name goods_name',
                'bps.service_cover goods_cover',
                'goods_price',
                'bps.service_unit goods_price_show',
                'order_goods_number goods_number'
            ])
            ->leftJoin('Apps\Common\Models\BeautyParlor', 'bp.bp_id = shop_id', 'bp')
            ->leftJoin('Apps\Common\Models\BeautyParlorService', 'bps.service_id = goods_id', 'bps')
            ->where('order_id = :oid:', ['oid' => $orderId])
            ->execute()
            ->toArray();
        for($i = 0; $i < count($goodsList); $i++) {
            $goodsList[$i]['shop_medal_refund'] = \Apps\Common\Libs\BeautyParlor::getMedalRefund($goodsList[$i]['shop_medal_refund']);
            $goodsList[$i]['shop_medal_verify'] = \Apps\Common\Libs\BeautyParlor::getMedalVerify($goodsList[$i]['shop_medal_verify']);

            $goodsList[$i]['goods_cover'] = PicUrl::BeautyParlorCover($goodsList[$i]['goods_cover'], $this->getDi());

            $unit = $goodsList[$i]['goods_price_show'];
            if($goodsList[$i]['goods_price'] > 0) {
                $goodsList[$i]['goods_price_show'] = $goodsList[$i]['goods_price'] . '元' . ($unit ? '/' . $unit : '');
            } else {
                $goodsList[$i]['goods_price_show'] = '';
            }
        }
        return $goodsList;
    }
}